<template>
  <div class="container">
    <div class="app-container">
      <el-tree :expand-on-click-node="true" :data="depts" :props="defaultProps">

        <!-- 节点结构 -->
        <!-- v-slot="{ node, data }" 只能作用在template -->
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="hCommand($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>

      </el-tree>
    </div>
    <!-- 子组件 弹框-->
    <!-- 父传子 .sync语法糖 update -->
    <!-- @updateDepartment="getDepartment"子传值 重新获取列表 -->
    <add-dept ref="dept" :show-dialog.sync="showDialog" :cur-id="curId" :is-edit="isEdit" @updateDepartment="getDepartment" />
  </div>
</template>

<script>
import { delDepart, getDepartment } from '@/api/department'
import { arr2tree } from '@/utils'
import addDept from './components/addDept.vue'

export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false, // 控制 添加部门的显示和隐藏
      curId: '', // 存储当前点击的id  父级的id等于子级的pid
      isEdit: false// 判断是编辑还是添加
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 获取-部门列表
    async getDepartment() {
      const res = await getDepartment()
      // console.log(res)
      this.depts = arr2tree(res.data, 0)
    },
    hCommand(type, id) {
      this.curId = id
      // console.log(id)
      if (type === 'add') {
        // 显示弹框
        this.showDialog = true
        this.isEdit = false
        // console.log(this.isEdit)
      } else if (type === 'edit') {
        // 显示弹框
        this.showDialog = true
        this.isEdit = true
        // console.log(this.isEdit)

        this.$nextTick(() => {
          // 获取部门详情
          this.$refs.dept.updateList()
        })
        // 这里为什么要用$nextTick，因为弹出层之后紧接着要调用子组件方法，第一次弹出层时，里面的组件还未完成渲染，所以要等到showDialog这个属性设置的数据完成渲染之后才进行调用
      } else if (type === 'del') {
        this.$confirm('你确定要删除吗?')
          .then(async() => {
            await this.del(id)
          })
          .catch(() => {})
      }
    },
    // 删除-部门列表
    async del(id) {
      await delDepart(id)
      this.$message.success('删除成功')
      this.getDepartment()
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
